package com.eling.elcms.travel.dao.hibernate;

import com.eling.elcms.core.dao.hibernate.GenericDaoHibernate;
import com.eling.elcms.travel.dao.IRoomTypePriceDao;
import com.eling.elcms.travel.model.RoomTypePrice;
import org.springframework.stereotype.Repository;

import java.util.Date;

@Repository
public class RoomTypePriceDaoHibernate extends GenericDaoHibernate<RoomTypePrice, Long> implements IRoomTypePriceDao{

	public RoomTypePriceDaoHibernate() {
		super(RoomTypePrice.class);
	}

	@Override
	public void deleteOldPrice(RoomTypePrice roomTypePrice, Date startDate, Date endDate, String week) {
		StringBuilder sql = new StringBuilder();
		sql.append("	DELETE	");
		sql.append("	FROM	");
		sql.append("		travel_roomtypeprice	");
		sql.append("	WHERE	1=1 ");
		sql.append("	AND saleDate>=:startDate	");
		sql.append("	AND saleDate<=:endDate	");
		sql.append("	AND	pkorgbuilding = '"+roomTypePrice.getOrgBuilding().getPkOrgBuilding()+"'	");
		sql.append("	AND pkroomtype = '"+roomTypePrice.getRoomType().getPkRoomType()+"'	");
		sql.append("	AND DATE_FORMAT(saleDate, '%W') IN ("+week+")	");
		getSession().createNativeQuery(sql.toString()).setParameter("endDate", endDate).setParameter("startDate", startDate).executeUpdate();
	}
}
